package com.kchouse.myadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.kchouse.R;
import com.kchouse.bean.GivingItem;
import com.kchouse.bean.OrderItem;
import com.kchouse.bean.ProjectList;
import com.kchouse.common.MyUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class OrderAllAdapter extends RecyclerView.Adapter<OrderAllAdapter.ViewHolder> {

    private ArrayList<OrderItem> datas;
    private Context context;


    public OrderAllAdapter(Context context, ArrayList<OrderItem> datas) {
        this.datas = datas;
        this.context = context;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {

        View view = null;
        ViewHolder vh = null;
        view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.lv_orderall_item, viewGroup, false);
        vh = new ViewHolder(view);
        return vh;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {


        final OrderItem item = datas.get(position);


        if (TextUtils.isEmpty(item.getSuitUrl())) {
            Picasso.with(context).load("null").error(R.mipmap.ic_loadingerror).into(viewHolder.iv_suitUrl);
        } else {
            Picasso.with(context).load(item.getSuitUrl()).error(R.mipmap.ic_loadingerror).into(viewHolder.iv_suitUrl);
        }
//
        viewHolder.tv_merchantName.setText(item.getMerchantName());
        viewHolder.tv_createTime.setText(item.getCreateTime().substring(0, 10));
        viewHolder.tv_suitName.setText(item.getSuitName());

        StringBuffer sbgiving = new StringBuffer();
        for (int i = 0; i < item.getGivingList().size(); i++) {
            GivingItem givitem = item.getGivingList().get(i);
            sbgiving.append(givitem.getProjectName() + givitem.getNum() + "次\n");
        }

        if (TextUtils.isEmpty(sbgiving.toString())) {
            viewHolder.tv_giving.setText("");
            viewHolder.tv_1.setVisibility(View.GONE);
        } else {
            viewHolder.tv_1.setVisibility(View.VISIBLE);
            viewHolder.tv_giving.setText(sbgiving.toString());
        }
        viewHolder.tv_price.setText("实付款：" + MyUtils.getdouble(item.getPrice()));


        if (!item.isSuitStatus()) {
            viewHolder.tv_isfinished.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tv_isfinished.setVisibility(View.GONE);
        }
    }


    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tv_merchantName;
        public TextView tv_createTime;
        public ImageView iv_suitUrl;
        public TextView tv_isfinished;
        public TextView tv_suitName;
        public TextView tv_giving;
        public TextView tv_price;
        public TextView tv_1;

        public ViewHolder(View view) {
            super(view);
            tv_isfinished = (TextView) view.findViewById(R.id.tv_isfinished);
            tv_merchantName = (TextView) view.findViewById(R.id.tv_merchantName);
            tv_createTime = (TextView) view.findViewById(R.id.tv_createTime);
            tv_suitName = (TextView) view.findViewById(R.id.tv_suitName);
            iv_suitUrl = (ImageView) view.findViewById(R.id.iv_suitUrl);
            tv_giving = (TextView) view.findViewById(R.id.tv_giving);
            tv_price = (TextView) view.findViewById(R.id.tv_price);
            tv_1 = (TextView) view.findViewById(R.id.tv_1);
        }
    }


}
